package com.ruipengkj.model;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TPurchaseReturn;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.service.TEmployeeService;

public class PurchaseRetreatModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private Vector<List<Object>> rowdatas=new Vector<List<Object>>();
	private Vector<String> columns = new Vector<String>();
	
	private TEmployeeService tEmployeeService;
	public PurchaseRetreatModel(){
		tEmployeeService = BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		columns.add("退货单号");
		columns.add("退货人名称");
		columns.add("退货时间");
		columns.add("操作人");
		columns.add("退货原因");
	}
	//获得行数
	public int getRowCount(){
		return rowdatas.size();
	}
	//获得列数
	public int getColumnCount(){
		return columns.size();
	}
	//获得单元格的值
	public Object getValueAt(int row,int column){
		
		return ((Vector)rowdatas.get(row)).get(column);
	}
	//获取列名
	public String getColumnName(int column){
		return (String)columns.get(column);
	}

	//设置单元格是否可编辑
	public boolean isCellEditable(int rowIndex,int columnIndex){
		return false;
	}
	
	public void removeData(int row){
		rowdatas.remove(row);
	}
	
	//设置单元格的值
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		//取出对应的记录
		Vector rowdata = (Vector)rowdatas.get(rowIndex);
		//删除原来的值
		rowdata.remove(columnIndex);
		//添加用户新修改的值
		rowdata.add(columnIndex,aValue);
	}


	public void setRowData(List<TPurchaseReturn> tPurchaseReturnList) {
		rowdatas.clear();
		if(tPurchaseReturnList!=null&&tPurchaseReturnList.size()!=0){
			for(int i=0;i<tPurchaseReturnList.size();i++){
				TPurchaseReturn TPurchaseReturn=tPurchaseReturnList.get(i);
				List<Object> row=new Vector<Object>();
				row.add(TPurchaseReturn.getReturnID());
				if (TPurchaseReturn.getEmployee() !=null && !TPurchaseReturn.getEmployee().equals("")){
					try {
						TEmployee tEmployee = tEmployeeService.getNameByIdService(TPurchaseReturn.getEmployee());
						if (tEmployee != null && !tEmployee.getName().equals(""))
							row.add(tEmployee.getName());
						else
							row.add("");
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					row.add("");
				}
				row.add(TPurchaseReturn.getHappenTime());
				if(TPurchaseReturn.getOperator() != null){
					try {
						TEmployee tEmployee = tEmployeeService.getNameByIdService(TPurchaseReturn.getEmployee());
						if (tEmployee != null && !tEmployee.getName().equals(""))
							row.add(tEmployee.getName());
						else
							row.add("");
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					row.add("");
				}
				row.add(TPurchaseReturn.getReason());
				rowdatas.add(row);
			}
		}
	}

	public void clear(){
		rowdatas.clear();
	}
}
